<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class ListController extends BaseController {
	public function _initialize(){
	}
    public function news(){		
	  parent::index("news");
    }
	public function practices(){		
	  parent::index("practices","yjl_check","1");
    }
	public function groups(){		
	  parent::index("groups","yjl_check","1");
    }
	public function shownew(){
		parent::mod("news","yjl_id");
	}
	public function showpractice(){
		$id=I('get.id');//获取id--详细信息
		//var_dump($id);
		//点击量加一
		$model=M("practices");
		$model->where("yjl_id = '{$id}'")->setInc('yjl_hit');

		$data=$model->where("yjl_id='{$id}'")->find();
		$this->assign("firblock",$data);
			
		//社团名称
		$sql="select * from yjl_groups where yjl_id='{$id}'";
		$data2=M()->query($sql);
		$this->assign("lem",$data2);//社团和发布者名称
		//已报名人员
			$sql="select * from yjl_users where yjl_stunum in (
			select yjl_stunum from yjl_stuprac where yjl_pid='{$id}' and yjl_check=1) ";
			$data1=M()->query($sql);
			$this->assign("secblock",$data1);
			
		//查询是否已经报名
		if(isset($_SESSION['user'])){
			
			$sql="select * from yjl_stuprac where yjl_stunum='{$_SESSION['user']['yjl_stunum']}' and yjl_pid='{$id}'";
			if($flag=M()->query($sql)){
				$pflag=2;//已经报名
			}else{
				$pflag=1;//未报名
			}
		}else{
			$pflag=0;
		}
		$this->assign("mystunum",$_SESSION['user']['yjl_stunum']);
		$this->assign("pflag",$pflag);
		$this->display();
	}
	public function showgroup(){
		$id=I('get.id');//获取id--详细信息
		$model=M("groups");
		$data=$model->where("yjl_id='{$id}'")->find();
		$this->assign("firblock",$data);
		
		//社团成员--members
			$sql="select * from yjl_users where yjl_stunum in (
			select yjl_stunum from yjl_members where yjl_gid='{$id}' and yjl_check=1)";
			$data1=M()->query($sql);
			$this->assign("secblock",$data1);
		
		//社团举办过的活动--practices
			$data2=M("practices")->where("yjl_gid='{$id}' and yjl_check=1 ")->select();
			$this->assign("thirblock",$data2);
			
		//查询是否已经报名
		if(isset($_SESSION['user'])){
			
			$sql="select * from yjl_members where yjl_stunum='{$_SESSION['user']['yjl_stunum']}' and yjl_gid='{$id}'";
			if($flag=M()->query($sql)){
				$pflag=2;//已经报名
			}else{
				$pflag=1;//未报名
			}
		}else{
			$pflag=0;
		}
		//var_dump($flag);exit;
		$this->assign("mystunum",$_SESSION['user']['yjl_stunum']);
		$this->assign("pflag",$pflag);
		
		$this->display();
	}
	public function joinin($tbl,$id){
		$model=D($tbl);
			if($tbl=="stuprac"){//报名参加活动
				$data=array(
					"yjl_stunum"=>"{$_SESSION['user']['yjl_stunum']}",
					"yjl_realname"=>"{$_SESSION['user']['yjl_realname']}",
					"yjl_pid"=>"{$id}"
				);
				$position="List/showpractice/id/{$id}";
				
			}else if($tbl=="members"){//报名参加社团
				$data=array(
					"yjl_stunum"=>"{$_SESSION['user']['yjl_stunum']}",
					"yjl_gid"=>"{$id}"
				);
				$position="List/showgroup/id/{$id}";
			}
			if($model->create($data)){
				$model->add();
				
				$this->success('报名成功！',U($position));
			}else{
				$this->error($model->getError());
				$this->error("报名失败！",U($position));
			}
			
	}
	public function deletelist($tbl,$id){//取消报名
		 $mode=M($tbl);
		if($tbl=="stuprac"){
			$stunum=$_SESSION['user']['yjl_stunum'];
			$ay=array(
				'yjl_pid'=>$id,
				'yjl_stunum'=>$stunum
			);
			$res=$mode->where($ay)->delete();
			$position="List/showpractice/id/{$id}";
			if($res){
				$this->success('取消报名成功！',U($position));
			}else{
				$this->error('取消报名失败！',U($position));
			}		
		}else if($tbl=="members"){
			$stunum=$_SESSION['user']['yjl_stunum'];
			$ay=array(
				'yjl_gid'=>$id,
				'yjl_stunum'=>$stunum
			);
			//var_dump($ay);exit;
			$res=$mode->where($ay)->delete();
			//var_dump($res);exit;
			$position="List/showgroup/id/{$id}";
			if($res){
				$this->success('取消报名成功！',U($position));
			}else{
				$this->error('取消报名失败！',U($position));
			}		
		}
		
	}
}